<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <button @click="str= 'Child1'">btn1</button>
      <button @click="str= 'Child2'">btn2</button>
      <button @click="str= 'Child3'">btn3</button>
      <hr />
      <!-- include表示包含 -->
      <!-- exclude表示排除 -->
      <!-- a,b指的是组件的name选项的值 -->

      <!-- max表示最大缓存数 -->
      <keep-alive exclude="child1,child2" :max="10">
        <component :is="str"></component>
      </keep-alive>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const Child1 = {
        name: "child1",
        template: `
          <div>这是child1子组件</div>
        `,
        mounted() {
          console.log("child1 mounted");
        },
        beforeUnmount() {
          console.log("child1 unMounted");
        },
      };
      const Child2 = {
        name: "child2",
        template: `
          <div>这是child2子组件</div>
        `,
        mounted() {
          console.log("child2 mounted");
        },
        beforeUnmount() {
          console.log("child2 unMounted");
        },
      };
      const Child3 = {
        name: "c",
        template: `
          <div>这是child3子组件</div>
        `,
        mounted() {
          console.log("child3 mounted");
        },
        beforeUnmount() {
          console.log("child3 unMounted");
        },
      };
      Vue.createApp({
        data() {
          return {
            str: "Child1",
          };
        },
        components: {
          Child1,
          Child2,
          Child3,
        },
      }).mount("#app");
    </script>
  </body>
</html>
